<?php
apf_require_class('DAO_DAO');
class Dao_Attendance_UserRole extends DAO {

    public function get_table_name() {
        return "user_role";
    }

    public function get_read_pdo_name() {
        return "attendance_slave";
    }

    public function get_write_pdo_name() {
        return "attendance_master";
    }

    public function get_table_pk() {
        return "user_id";
    }

    public function _find_short_assoc($where,$order="user_id asc",$limit = 0,$offset = 0) {
        apf_require_class("Dao_Attendance_UserInfo");
        $dao_userinfo = new Dao_Attendance_UserInfo();
        $w = "";
        if(!empty($where['city_name'])) {
            $w = "and u_i.city_name = '{$where['city_name']}'";
        }
        if(!empty($where['search_keywords'])) {
            $w .= " and (name like '{$where['search_keywords']}%' or code = '{$where['search_keywords']}')";
        }
        $s_sql = "select * from ".$dao_userinfo->get_table_name()." as u_i
                  left join ".$this->get_table_name()." as u_r
                  on u_r.user_id = u_i.user_id
                  where u_i.dimission_date='0000-00-00 00:00:00'
                  and u_r.role_id = {$where['role_id']} {$w}
                 limit {$limit} offset {$offset}";
        $rs = $this->execute($s_sql);
        $result = array();
        if(!empty($rs)) {
            foreach($rs as $key => $v) {
                $result[$v['user_id']] = $v;
            }
        }
        return $result;
    }

    public function _find_count($where = array()) {
        apf_require_class("Dao_Attendance_UserInfo");
        $dao_userinfo = new Dao_Attendance_UserInfo();
        $w = "";
        if(!empty($where['city_name'])) {
            $w = "and u_i.city_name = '{$where['city_name']}'";
        }
        if(!empty($where['search_keywords'])) {
            $w .= " and (name like '{$where['search_keywords']}%' or code = '{$where['search_keywords']}')";
        }
        $s_sql = "select count(*) as total_rows from ".$dao_userinfo->get_table_name()." as u_i
                  left join ".$this->get_table_name()." as u_r
                          on u_r.user_id = u_i.user_id
                          where u_i.dimission_date='0000-00-00 00:00:00'
                          and u_r.role_id = {$where['role_id']} $w";
        $rs = $this->execute($s_sql);
        return $rs['0']['total_rows'];
    }
}